/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.service;

import java.util.List;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.dao.CategoryDao;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import com.thinkgem.jeesite.modules.cms.entity.Category;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.cms.entity.ExVideo;
import com.thinkgem.jeesite.modules.cms.dao.ExVideoDao;

/**
 * 视频Service
 * @author 夏国强
 * @version 2016-05-26
 */
@Service
@Transactional(readOnly = true)
public class ExVideoService extends CrudService<ExVideoDao, ExVideo> {
    @Autowired
    private ExVideoDao videoDao;
    @Autowired
    private CategoryDao categoryDao;

	public ExVideo get(String id) {
		return super.get(id);
	}
	
	public List<ExVideo> findList(ExVideo exVideo) {
		return super.findList(exVideo);
	}
	
	public Page<ExVideo> findPage(Page<ExVideo> page, ExVideo exVideo) {
        if (exVideo.getCategory()!=null && StringUtils.isNotBlank(exVideo.getCategory().getId()) && !Category.isRoot(exVideo.getCategory().getId())){
            Category category = categoryDao.get(exVideo.getCategory().getId());
            if (category==null){
                category = new Category();
            }
            category.setParentIds(category.getId());
            category.setSite(category.getSite());
            exVideo.setCategory(category);
        }
        else{
            exVideo.setCategory(new Category());
        }
        return super.findPage(page, exVideo);
	}
	
	@Transactional(readOnly = false)
	public void save(ExVideo exVideo) {
        if (!UserUtils.getSubject().isPermitted("cms:article:audit")){
            exVideo.setDelFlag(ExVideo.DEL_FLAG_AUDIT);
        }
        // 如果栏目不需要审核，则将该内容设为发布状态
        if (exVideo.getCategory()!=null&&StringUtils.isNotBlank(exVideo.getCategory().getId())){
            Category category = categoryDao.get(exVideo.getCategory().getId());
            if (!Global.YES.equals(category.getIsAudit())){
                exVideo.setDelFlag(ExVideo.DEL_FLAG_NORMAL);
            }
        }
		super.save(exVideo);
	}
	
	@Transactional(readOnly = false)
	public void delete(ExVideo exVideo,Boolean isRe) {
        exVideo.setDelFlag(Boolean.TRUE.equals(isRe) ? Article.DEL_FLAG_NORMAL : Article.DEL_FLAG_DELETE);
		super.delete(exVideo);
	}

    public List<ExVideo> getVideoByCategoryId(String cateGoryId){ return videoDao.getVideoByCategoryId(cateGoryId);}
	
}